package com.baizhiedu.basic.service;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Date;

/**
 * @Author: robert.song
 * @Date: Created in 2022/4/11
 */

@AllArgsConstructor
@NoArgsConstructor
public class CycleTestService1 implements CycleTestInterface, BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {

    private CycleTestInterface cycleTestService2;

    private int i;

    public CycleTestInterface getCycleTestService2() {
        return cycleTestService2;
    }

    public void setCycleTestService2(CycleTestInterface cycleTestService2) {
        this.cycleTestService2 = cycleTestService2;
    }

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }

    @Override
    public void setBeanName(String name) {
        System.out.println(new Date() + " setBeanName");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println(new Date() + " setBeanFactory");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println(new Date() + " afterPropertiesSet");
        Thread.sleep(1000);
    }

    @PostConstruct
    public void postConstructor() {
        System.out.println(new Date() + " postConstructor");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @PreDestroy
    public void preDestroy() {
        System.out.println(new Date() + " preDestroy");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void initMethod() {
        System.out.println(new Date() + " init Method");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void destroyMethod() {
        System.out.println(new Date() + " destroy Method");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void destroy() {
        System.out.println(new Date() + " destroy");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
